<?php
/** @noinspection PhpUnused */
declare(strict_types=1);

namespace app\shop\controller;

use app\shop\exception\FailException;
use app\shop\exception\ParamsErrorException;
use app\shop\logic\UserAddressLogic;
use app\shop\validate\UserAddressValidate;
use think\response\Json;

/**
 * @property UserAddressLogic $logic
 */
class UserAddressController extends BaseController
{
    protected string $validateClassName = UserAddressValidate::class;

    public function __construct(UserAddressLogic $addressLogic)
    {
        parent::__construct();

        $this->logic = $addressLogic;
    }

    /**
     * 用户地址列表
     * @return Json
     * @throws ParamsErrorException
     */
    public function index(): Json
    {
        $params = $this->getInput(true);

        $this->validateData($params, 'list');

        $result = $this->logic->list($params);

        return $this->success($result);
    }

    /**
     * 用户地址详情
     * @return Json
     * @throws ParamsErrorException
     */
    public function detail(): Json
    {
        $params = $this->getInput(true);

        $this->validateData($params, 'detail');

        $result = $this->logic->detail($params);

        return $this->success($result);
    }

    /**
     * 添加用户地址
     * @return Json
     * @throws ParamsErrorException
     * @throws FailException
     */
    public function add(): Json
    {
        $params = $this->postInput(true);

        $this->validateData($params, 'add');

        $result = $this->logic->add($params);

        return $this->success($result);
    }

    /**
     * 修改用户地址
     * @return Json
     * @throws FailException
     * @throws ParamsErrorException
     */
    public function edit(): Json
    {
        $params = $this->postInput();

        $this->validateData($params, 'edit');

        $result = $this->logic->edit($params);

        return $this->success($result);
    }

    /**
     * 删除用户地址
     * @throws FailException
     * @throws ParamsErrorException
     */
    public function delete(): Json
    {
        $params = $this->postInput();

        $this->validateData($params, 'delete');

        $result = $this->logic->delete($params['id']);

        return $this->success($result);
    }
}
